//
//  ImportAudioViewController.m
//  BDD_TWO
//
//  Created by XTW on 2023/6/9.
//

#import "ImportAudioViewController.h"
#import "FileManager.h"
#import "LocalRecordModel.h"

@interface ImportAudioViewController ()<UIDocumentPickerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *actionBtn;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation ImportAudioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"导入音频";
    [self addBackBtn];
    [self initView];
    // Do any additional setup after loading the view from its nib.
}

- (void)initView {
    [self.scrollView setFrame:CGRectMake(0, IS_iPhoneX_Top + NavBarHeight, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Top - NavBarHeight)];
    
    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = TextColor.CGColor;
    border.fillColor = nil;
    border.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE - 32, 186) cornerRadius:12.0].CGPath;
    border.frame = CGRectMake(0, 0, SCREEN_WIDTH_DEVICE - 32, 186);
    border.lineWidth = 1.0;
    border.lineCap = @"square";
    // 设置线宽和线间距
    border.lineDashPattern = @[@4, @4];
    [self.actionBtn.layer addSublayer:border];
}


- (IBAction)surePress {
    UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.audio"] inMode:UIDocumentPickerModeOpen];
    documentPickerViewController.delegate = self;
    [[UIViewControllerCJHelper findCurrentShowingViewController] presentViewController:documentPickerViewController animated:YES completion:nil];
}


#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls {
    NSURL *url = [urls firstObject];
    BOOL fileUrlAuthozied = [url startAccessingSecurityScopedResource];
    if (fileUrlAuthozied) {
        //通过文件协调工具来得到新的文件地址，以此得到文件保护功能
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;
        [fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
            //读取文件
            NSString *fileName = [newURL lastPathComponent];
            FileManager *manager = [FileManager sharedManager];
            LocalRecordModel *localModel = [[LocalRecordModel alloc] initImport:fileName];
            NSString *path = [manager getLocalFilePath:localModel.fileName];
            NSURL *destURL = [NSURL fileURLWithPath:path];
            NSError *error;
            BOOL success = [[NSFileManager defaultManager] copyItemAtURL:newURL toURL:destURL error:&error];
            if (success) {
                [UserManger createFileModel:localModel];
                [self dismissViewControllerAnimated:YES completion:^{
                    if (self.type == 1) {
                        if (self.ReloadHandle) {
                            self.ReloadHandle();
                        }
                    } else {
                        [self.navigationController popToRootViewControllerAnimated:NO];
                    }
                }];
            }
        }];
        [url stopAccessingSecurityScopedResource];
    }
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    return self;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller {
    return self.view;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller {
    return self.view.frame;
}

@end
